package com.yunchao.feature.business.presentation.screen.info

import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import by.kirich1409.viewbindingdelegate.viewBinding
import com.yunchao.feature.base.presentation.activity.BaseFragment
import com.yunchao.feature.base.presentation.ext.hideLoadingDialog
import com.yunchao.feature.base.presentation.ext.setDebouncedClickListener
import com.yunchao.feature.base.presentation.ext.showLoadingDialog
import com.yunchao.feature.base.presentation.ext.toast
import com.yunchao.feature.business.R
import com.yunchao.feature.business.databinding.FragmentBusinessInfoBinding
import com.yunchao.feature.business.domain.model.BusinessInfoModel
import com.yunchao.feature.business.domain.model.getTranTypeName
import kotlinx.coroutines.launch
import org.koin.androidx.navigation.koinNavGraphViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import kotlin.getValue

class BusinessInfoFragment : BaseFragment(R.layout.fragment_business_info) {

    private val binding: FragmentBusinessInfoBinding by viewBinding<FragmentBusinessInfoBinding>()
    private val viewModel: BusinessInfoViewModel by koinNavGraphViewModel(R.id.businessNavGraph)
    private var businessInfoModel: BusinessInfoModel? = null

    override fun initView(view: View, savedInstanceState: Bundle?) {

        binding.taskRefreshTv.setDebouncedClickListener { getBusinessInfo() }

        binding.taskTypeTv.setDebouncedClickListener {
            businessInfoModel?.apply { viewModel.navigateToDetails(this) }
        }

        lifecycleScope.launch { viewModel.uiStateFlow.collect(::render) }
        lifecycleScope.launch { viewModel.uiEffectFlow.collect(::sideEffect) }

    }

    private fun render(state: BusinessInfoViewModel.UiState) {
        when (state) {
            is BusinessInfoViewModel.UiState.Idle ->{
                businessInfoModel = null
                binding.apply {
                    taskTypeTv.text = getString(com.yunchao.feature.base.R.string.暂无任务)
                    taskOperatorTv.text = getString(
                        com.yunchao.feature.base.R.string.操作人信息
                    )
                    taskAuthorTv.text = getString(
                        com.yunchao.feature.base.R.string.授权人信息
                    )
                }
            }

            is BusinessInfoViewModel.UiState.Success -> {
                this.businessInfoModel = state.businessModel
                binding.apply {
                    taskTypeTv.text = state.businessModel.getTranTypeName()
                    taskOperatorTv.text = getString(
                        com.yunchao.feature.base.R.string.操作人STR,
                        state.businessModel.giveOperator
                    )
                    taskAuthorTv.text = getString(
                        com.yunchao.feature.base.R.string.授权人STR,
                        state.businessModel.chkUserId
                    )
                }
            }
        }
    }


    private fun sideEffect(effect: BusinessInfoViewModel.UiEffect) {
        when (effect) {
            is BusinessInfoViewModel.UiEffect.ShowLoading -> showLoadingDialog()
            is BusinessInfoViewModel.UiEffect.HideLoading -> hideLoadingDialog()
            is BusinessInfoViewModel.UiEffect.ShowToast -> toast(effect.msg)
        }
    }

    fun getBusinessInfo() {
        viewModel.getBusinessInfo()
    }
}